//
// C++ Implementation: qtaddinterface
//
// Description:
//
//
// Author: Willi Ballenthin <wilbal1087@gmail.com>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "activityeditwidget.h"
#include <QDebug>


/**
* Constructor.
* @param act An example activity of the type this instance of the application
* should use, ie SqlActivity.
*/
ActivityEditWidget::ActivityEditWidget (QSqlDatabase* db, QWidget *parent )
        : QWidget ( parent )
{
    this->db = db;

    ui.setupUi ( this );

    connect ( ui.pb_insert, SIGNAL ( clicked() ), this, SLOT ( submit() ) );
    connect ( ui.pb_clear,  SIGNAL ( clicked() ), this, SLOT ( clear () ) );
}


ActivityEditWidget::~ActivityEditWidget()
{
}

/**
* Collects the data from the input widgets and submits this data
* to the database.  If the Activity has not yet been inserted, then
* the data is inserted as a new activity. Else, the Activity is updated.
*/
void ActivityEditWidget::submit()
{
    activity->setDate(ui.cal_calendar->selectedDate());
    activity->setTime(ui.te_time->time());

    int hours = ui.sb_hours->value();
    int mins  = ui.sb_mins ->value();
    int secs  = ui.sb_secs ->value();
    QTime duration(hours,mins,secs, 0);
    activity->setDuration(duration);

    activity->setDistance(ui.dsb_distance->value());
    activity->setEffort(ui.sb_effort->value());
    activity->setLocation(ui.le_location->text());
    activity->setType(ui.cb_type->currentText());
    activity->setRoute(ui.le_route->text());
    activity->setNotes(ui.le_notes->text());
    activity->setSplits(ui.le_splits->text());

    // whether to update or insert is taken care of
    // by the activity method
    activity->insert(db);

    ui.l_status->setText("Activity inserted.");
    QTimer::singleShot(4000, this, SLOT(clearStatusMessage()));

    emit updatedDb();
    emit updatedModel();
    this->setEnabled(false);
}


/**
* Clears the data from the current Activity.
* If the activity is only being edited, not composed,
* the changes are dropped, but the old data is not deleted.
* If the Activity is new, then all previous data is deleted.
*/
void ActivityEditWidget::clear()
{
    if(activity->getId() == 0)
    // new activity.
    {
        // destroy old data
        delete activity;
        // allocate a new activity
        activity = new Activity;
    }
    else
    {
        // lose the pointer to the old data
        // and allocate a new activity
        activity = new Activity;
    }

    updateView();

    ui.l_status->setText("Activity cleared.");
    QTimer::singleShot(4000, this, SLOT(clearStatusMessage()));
}

/**
* Slot that clears the label showing current status.
*/
void ActivityEditWidget::clearStatusMessage()
{
    ui.l_status->setText("");
}

/**
* Slot that sets the current Activity as a previous Activity
* that needs to be updated/edited.
* @param activity A reference to the activity that needs to be updated.
*/
void ActivityEditWidget::edit(Activity* activity)
{
    this->setEnabled(true);
    this->activity = activity;
    updateView();
}

/**
* Updates the data fields of the widget.
*/
void ActivityEditWidget::updateView()
{
    if(activity->getId() == 0)
    {
        ui.pb_insert->setText("Insert");
    }
    else
    {
        ui.pb_insert->setText("Update");
    }

    ui.cal_calendar->setSelectedDate(activity->getDate());
    ui.te_time->setTime(activity->getTime());
    ui.sb_hours->setValue(activity->getDuration().hour());
    ui.sb_mins->setValue(activity->getDuration().minute());
    ui.sb_secs->setValue(activity->getDuration().second());
    ui.dsb_distance->setValue(activity->getDistance());
    QString type = activity->getType();
    int typeIndex = ui.cb_type->findText(type);
    ui.cb_type->setCurrentIndex(typeIndex);
    ui.sb_effort->setValue(activity->getEffort());
    ui.le_route->setText(activity->getRoute());
    ui.le_location->setText(activity->getLocation());
    ui.le_notes->setText(activity->getNotes());
    ui.le_splits->setText(activity->getSplits());
}


